//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using Style = CommunityServer.Controls.Style;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for CategoryForm.
	/// </summary>
	public class LinkCategoryForm : BaseBlogPage
	{
		protected Style ControlPanelStyle;
		protected FormLabel tt;
		protected TextBox CategoryName;
		protected FormLabel Formlabel1;
		protected TextBox CategoryDesc;
		protected FormLabel Formlabel2;
		protected CheckBox CategoryEnabled;
		protected RequiredFieldValidator RequiredFieldValidator1;
		protected MPContent bcr;
		protected IButton Button1;
	
		private void Page_Load(object sender, EventArgs e)
		{
            if(!IsPostBack)
            {
            	LinkCategory lc = GetCategory();	        
                CategoryName.Text = lc.Name;
                CategoryDesc.Text = lc.Description;
                CategoryEnabled.Checked = lc.IsEnabled;
            }

			Head.AddTitle(ResourceManager.GetString("CP_Blog_Links_CategoryTitle"), this.Context);
		}

        protected LinkCategory GetCategory()
        {
            LinkCategory lc = null;

            if(Request.QueryString["CategoryID"] != null && Request.QueryString["CategoryID"] != "-1")
            {
                lc = LinkCategories.GetCategory(Int32.Parse(Request.QueryString["CategoryID"]),CurrentWeblog.SectionID,true);
                if(lc == null)
                    throw new Exception("Category could not be found");
            }
            else
            {
               lc = new LinkCategory();
                lc.SectionID = this.CurrentWeblog.SectionID;
            
            }

            return lc;
        }

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new EventHandler(this.Button1_Click);
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, EventArgs e)
		{
            if(Page.IsValid)
            {
                LinkCategory lc = GetCategory();
                lc.Name = CategoryName.Text;
                lc.Description = CategoryDesc.Text;
                lc.IsEnabled = CategoryEnabled.Checked;


                string resultArray = "new Array({0},\"{1}\",{2})";
                if(lc.LinkCategoryID > 0)
                {
                    string result = string.Format(resultArray,"false",lc.Name.Replace("\"","@@@"),lc.LinkCategoryID);
                    LinkCategories.UpdateCategory(lc);

					Modal.ClosePage(Page,result) ;
				}
                else
                {
                    lc.LinkCategoryID = LinkCategories.CreateCategory(lc);
                    string result = string.Format(resultArray,"true",lc.Name.Replace("\"","@@@"),lc.LinkCategoryID);
					
					Modal.ClosePage(Page,result) ;
				}

                
                
            }
			
		}
	}
}
